class Solution
{
public:
    bool closeStrings(string word1, string word2)
    {
        int n = word1.size();
        if (n != word2.size())
        {
            return false;
        }

        vector<int> count1(128, 0);
        vector<int> count2(128, 0);
        vector<int> type1(128, 0);
        vector<int> type2(128, 0);
        for (int i = 0; i < n; ++i)
        {
            ++count1[word1[i]];
            ++count2[word2[i]];
            type1[word1[i]] = 1;
            type2[word2[i]] = 1;
        }

        sort(count1.begin(), count1.end());
        sort(count2.begin(), count2.end());

        return count1 == count2 && type1 == type2;
    }
};